#include "comm.h"
//#include "r_cg_sau.h"
#include "machine_stat.h"
#include "string.h"

//#include "sys_process.h"
#include "display.h"
#include "custom_define.h"
#include "open_power_code.h"
#include "protocal.h"
//#include "comm_wifi.h"
extern uint8_t WIFI_IsConnected(void);
extern uint8_t WifiVersion[]; 
extern uint8_t dis_runmode;
extern uint8_t commfail_cnt;
extern uint8_t Commfail_Flag;
//extern uint8_t send_buf[60];

extern uint8_t wifi_cmd;
extern uint8_t cmd_times;
extern volatile uint8_t wifi_UART_send;
extern uint8_t cycle_flag;
extern OPENPOWER_CODE_STRUCT openpower_code;

uint8_t pack_buf[60]={0};
uint8_t get_buff[60]={0};
//uint8_t get_f2_buff[60]={0};
//uint8_t get_f2_buf[60]={0};
//uint8_t check_buf[30]={0};
//uint8_t jiji[100];
uint8_t com_timeout=0;
//uint8_t com_f2_timeout=0;
uint8_t comm_stat=0;
uint8_t rev_flag=0;
uint8_t sendover_flag=0;
uint8_t key_num = 0;
uint8_t numberaa=0;
//extern uint8_t self_de ;
//extern uint8_t self_in ;
extern uint8_t key_flag;
//uint8_t dis_cmd_times=0;
//uint8_t number=0;
//uint8_t nu1=0;
//上�?�主板发来的INF，和这�?�的进�?�比较，如果相同则是1；不同是0
//uint8_t last_check_num=1;
UART1_SEND uart1_send_flag;

uint8_t F2_Data[F2_DATA_SIZE]={0};
uint8_t F0_Data[F0_DATA_SIZE]={0};
extern  void wifi_f0_00_data(uint8_t massage);
uint8_t PassFlag = 0;
uint8_t bHeatTempIgnore = 0; /* 1: 不要将setheattemp 更新到Send_SetHeatTemp*/ 

void comm_rinnai_init(void)
{
	memset(pack_buf,0,sizeof(pack_buf));
	memset(get_buff,0,sizeof(get_buff));
	//USCI1_HANDLE_USCI1.TxState=USCI1_STATE_READY;
	uart1_send_flag.send_flag=SEND1_END;

}


 
void uart_get_F0_message(void)
{
	uint8_t length;
	
		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_MACHINE_DATA;  //data1
		pack_buf[5] = 0x00;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //
}

void uart_get_F1_message(void)
{
	uint8_t length;

		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_RUN_STATE;  //data1
		pack_buf[5] = 0x00;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //
}
/** @brief  F1 data process
 * @param rx: UART接收缓存区, 指向STX
 * @retval None
 */
void F1_Received_Process(uint8_t *rx){
    heater_info.err_code_h=rx[5+3];
	heater_info.err_code_l=rx[5+4];
    heater_info.err_type=rx[5+5];
	heater_info.fire_stat=rx[5+6];



	if(key_flag==0x00){
	    heater_info.SetHeatTemp=(rx[6]<<3)+(rx[6]<<1)+rx[7];
        heater_info.temp_h=rx[6];
	    heater_info.temp_l=rx[7];

		
		heater_info.Sendtemp_h=heater_info.temp_h;
		heater_info.Sendtemp_l=heater_info.temp_l;
		
		heater_info.power_stat=rx[5+7];
        heater_info.eco_stat=rx[5+8];
        heater_info.eco_mode=rx[5+9];

		/* 强制把温度下限设为35度 */

	    if(heater_info.running_mode==RUN_PLAIN){
	        heater_info.SetHeatTempMax=(rx[5+10]<<3)+(rx[5+10]<<1)+rx[5+11];
	        heater_info.SetHeatTempMin=(rx[5+12]<<3)+(rx[5+12]<<1)+rx[5+13];			
			if(heater_info.SetHeatTempMin <350)
                heater_info.SetHeatTempMin= 350;	
	    }else if(heater_info.running_mode == RUN_KITCHEN){
			heater_info.SetHeatTempMax=420;
			heater_info.SetHeatTempMin=350;
		}else if(heater_info.running_mode == RUN_SHOWER){
			heater_info.SetHeatTempMax=450;
			heater_info.SetHeatTempMin=350;	
		}

        /* 一键循环时如果最高温度超过48度,则强制设为48度 */
        if(heater_info.akey_cycle==0x31){
			if(heater_info.SetHeatTempMax>480)
			    heater_info.SetHeatTempMax = 480;
		}
		if(bHeatTempIgnore == 0)
		    heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;
        
        //if(heater_info.Send_SetHeatTemp > heater_info.SetHeatTempMax)
		//    heater_info.Send_SetHeatTemp = heater_info.SetHeatTempMax;
		//else if(heater_info.Send_SetHeatTemp < heater_info.SetHeatTempMin)	
		//    heater_info.Send_SetHeatTemp = heater_info.SetHeatTempMin;

        if(rx[5+14] == 0x30 && rx[5+16] == 0x30){
            heater_info.akey_cycle = 0x30;
            heater_info.super_mode = 0x30;
        }else if(rx[5+14] == 0x31 && rx[5+16] == 0x31){
            heater_info.akey_cycle = 0x31;
            heater_info.super_mode = 0x31;			
        }else if(rx[5+14] == 0x31){
            heater_info.akey_cycle = 0x31;
			heater_info.super_mode = heater_info.send_super_mode; 
        }else if(rx[5+16] == 0x31){
            heater_info.super_mode = 0x31;
		    heater_info.akey_cycle = heater_info.send_akey_cycle;
        }
        heater_info.send_super_mode = heater_info.super_mode;
        heater_info.send_akey_cycle = heater_info.akey_cycle;

        heater_info.cycle_mode = rx[5+15];
		heater_info.cold_stat=rx[5+17];
		heater_info.cool_and_hot=rx[5+19];
		heater_info.bathtub_mode=rx[5+20];
		heater_info.BathtubSetWater = rx[5+21];
		heater_info.BathtubRemainWater = rx[5+22];


	}

	//heater_info.err_code_l=0x00;
	heater_info.err_code_l_h=heater_info.err_code_l>>4;
	heater_info.err_code_l_l=heater_info.err_code_l&0x0f;
    
}

void uart_get_F2_message(void)
{
	uint8_t length;
	
		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_SERVICE_DATA;  //data1
		pack_buf[5] = 0x01;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //

}
/**  @brief send F4 Command to mainboard
 */
void MB_SetCommand(uint8_t cmd){
	key_flag = 1;
	key_num = cmd;
}

void uart_get_F4_message(uint8_t f4_cmd)
{
	int8_t length = -1;
	uint8_t *p = pack_buf+4;
	switch ( f4_cmd)
	{
		case 0:  //notouch
			length = 4;
			p[0] = 0xf4;  //data1
	    	p[1] = 0x03;  //data2
			p[2] = heater_info.Sendtemp_h;//(uint8_t)(heater_info.Send_SetHeatTemp/10);//userGetHeatTemp();
			p[3] = heater_info.Sendtemp_l;//(uint8_t)(heater_info.Send_SetHeatTemp%10);
	    break;
		case 1:  //SET_KEY_POWER
			length = 3;	
            p[0] = 0xf4;  //data1			
			p[1] = 0x00;//data2
			p[2] = 0x01;			
		break;       
	    case 2:  //SET_KEY_ECO
			length = 3;	  
            p[0] = 0xf4;  //data1			
			p[1] = 0x01;//data2
			p[2] = 0x01;
	    break;          
 	    case 3:  //key_de   key_in
			length = 4;
	    	p[0] = 0xf4;  //data1
	    	p[1] = 0x03;//data2
            //if(bHeatTempIgnore == 0){
				if(heater_info.Send_SetHeatTemp > heater_info.SetHeatTempMax)
					heater_info.Send_SetHeatTemp = heater_info.SetHeatTempMax;
				else if(heater_info.Send_SetHeatTemp < heater_info.SetHeatTempMin)	
					heater_info.Send_SetHeatTemp = heater_info.SetHeatTempMin;
			//}
			heater_info.Sendtemp_h=heater_info.Send_SetHeatTemp/10;
			heater_info.Sendtemp_l=heater_info.Send_SetHeatTemp%10;
			p[2] = heater_info.Sendtemp_h;//(uint8_t)(heater_info.Send_SetHeatTemp/10);//userGetHeatTemp();
			p[3] =  heater_info.Sendtemp_l;//(uint8_t)(heater_info.Send_SetHeatTemp%10);
	    break;      
		case 4:  //send_akeycycle
            if(heater_info.send_akey_cycle == 0x30){
				length = 3;
				p[0] = 0xF4;
				p[1] = SET_F4_CYCLE_ENA;
				p[2] = 0x31;
                Get_Time(TIME_CLEAN, &cycle_time); 
                heater_info.send_akey_cycle = 0x31;                
            }else if(heater_info.send_akey_cycle == 0x31 && heater_info.send_super_mode == 0x31){
					length = 3;
					p[0] = 0xF4;
					p[1] = SET_F4_SUPER_ENA;
					p[2] = 0x31; 
                    heater_info.send_akey_cycle = 0x30;
			}else{
				length = 3;
				p[0] = 0xF4;
				p[1] = SET_F4_CYCLE_ENA;
				p[2] = 0x30;  
                heater_info.send_akey_cycle = 0x30;                 
            }
            //if(heater_info.akeycycle==0x31){ /* Cycle Open */
			// 	if(heater_info.SetHeatTemp > TEMP_INCREASE_1)
			//		Cycle_open_save_temp=TEMP_INCREASE_1;
			//	else
			//		Cycle_open_save_temp=heater_info.SetHeatTemp;
			//}			
	       break;
		case 5:  //send_cycle_mode
			length = 3;
			p[0] = 0xf4;  //data1
			p[1] = 0x05;//data2
			p[2] = heater_info.send_cycle_mode;//
	    break;
		case 6:  //send_super_mode			
             if(heater_info.send_super_mode == 0x30){
				length = 3;
				p[0] = 0xF4;
				p[1] = SET_F4_SUPER_ENA;
				p[2] =0x31; 
                heater_info.send_super_mode = 0x31;
            }else if(heater_info.send_akey_cycle == 0x31 && heater_info.send_super_mode == 0x31){
				length = 3;
				p[0] = 0xF4;
				p[1] = SET_F4_CYCLE_ENA;
				p[2] =0x31; 
                Get_Time(TIME_CLEAN, &cycle_time); 
                heater_info.send_super_mode = 0x30;
			}else{
                length = 3;
				p[0] = 0xF4;
				p[1] = SET_F4_SUPER_ENA;
				p[2] =0x30;
                heater_info.send_super_mode = 0x30;                
            }
	    break;
		case 7:  //send_running_mode
            if(heater_info.cool_and_hot==0x31){
			    length = 3;
			    p[0] = 0xf4;  //data1
			    p[1] = 0x0d;  //data2
			    p[2] = 0x30;  //
            }
            heater_info.cool_and_hot = 0x30;            
	    break;
		case 8:  //send_running_mode
			length = 3;
			p[0] = 0xf4;  //data1
			p[1] = 0x02;//data2
			p[2] = heater_info.eco_mode;//
	    break;
		case 0x0b:  //浴缸
			length = 3;
			p[0] = 0xf4;  //data1
			p[1] = 0x0b;  //data2
			p[2] = 0x01;
            SetRunMode(RUN_PLAIN);
	    break;
		case 0x0c:  //send_running_mode
			length = 3;
			p[0] = 0xf4;  //data1
			p[1] = 0x0c;//data2
			p[2] = heater_info.BathtubSetWater;//
	    break;
		case 0x0d:  // Thermal Shock
			length = 3;
			p[0] = 0xf4;  //data1
			p[1] = 0x0d;//data2
			p[2] = heater_info.cool_and_hot;
            SetRunMode(RUN_PLAIN);
	    break;
       }

    if(length != -1){
        pack_buf[0] = 0x02;          //STX
		pack_buf[1] = 0x09;          //type
		pack_buf[2] = length;        //length
		pack_buf[3] = TEXT_SET;      //command
		pack_buf[4+length] = 0x03;   //ETX
		pack_buf[4+length+1] = checksum(pack_buf+1,pack_buf[2]+4); //
        UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
	}		
}

void uart_ack_message(void)
{
	uint8_t length;
	
	length = 0;
	pack_buf[0] = 0x02;
	pack_buf[1] = 0x09;
	pack_buf[2] = length;
	pack_buf[3] = 0x41;
	pack_buf[4] = 3;  //data1
	pack_buf[5] = checksum(pack_buf+1,pack_buf[2]+4); //

}
#if 1 /* 工程模式相关*/
void uart_resp_key_message(void){
uint16_t k = GetKeyboard();	
    pack_buf[0] = 0x02;          //STX
	pack_buf[1] = 0x09;          //type
	pack_buf[2] = 4;             //length
	pack_buf[3] = TEXT_RES;      //command
	pack_buf[4] = 0xFF;
	pack_buf[5] = 0x02;
	pack_buf[6] = (k>>8)&0xff;
	pack_buf[7] = (k)&0xff;
	pack_buf[8] = 0x03;   //ETX
	pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
    UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
}

void uart_resp_wifi_version(void){
    pack_buf[0] = 0x02;          //STX
	pack_buf[1] = 0x09;          //type
	pack_buf[2] = 8;             //length
	pack_buf[3] = TEXT_RES;      //command
	pack_buf[4] = 0xFF;
	pack_buf[5] = 0x03;
	pack_buf[6] = WIFI_IsConnected();
	pack_buf[7] = WifiVersion[0];
	pack_buf[8] = WifiVersion[1];
	pack_buf[9] = WifiVersion[2];
	pack_buf[10] = WifiVersion[3];
	pack_buf[11] = Rinnai_VERSION_NUMBER;
	pack_buf[12] = 0x03;   //ETX
	pack_buf[13] = checksum(pack_buf+1,pack_buf[2]+4); //
    UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
}

void uart_resp_time(void){
    pack_buf[0] = 0x02;          //STX
	pack_buf[1] = 0x09;          //type
	pack_buf[2] = 5;             //length
	pack_buf[3] = TEXT_RES;      //command
	pack_buf[4] = 0xFF;
	pack_buf[5] = 0x04;
	pack_buf[6] = time_stat.day_hour;
	pack_buf[7] = time_stat.day_min;
	pack_buf[8] = time_stat.day_sec;
	pack_buf[9] = 0x03;   //ETX
	pack_buf[10] = checksum(pack_buf+1,pack_buf[2]+4); //
    UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);	
}
#if  0  /* todo： 本意想独立出一个函数出来，暂时不改了 */
uint8_t FactoryFunction(uint8_t *rx_buf){
	if(rx_buf[3]==TEXT_SET && rx_buf[4]==0xFF){
		 if(get_buff[5] == 0x00){  /* 进入工程模式 */
              if(get_buff[6] == 0x31){
			      EnterFactoryMode(0x31);
				  comm_stat = 16;
			  }		  
		 }else if(get_buff[5] == 0x01){ /* 设置LED显示 */
              display_led(get_buff+6);
			  comm_stat = 16;
		 }
         get_buff[3]= 0;
         com_timeout = 0;
	}else if(get_buff[3] == TEXT_GET && get_buff[4] ==0xFF){
	     if(get_buff[5] == 0x02) { /* 获取按键值*/
              uart_resp_key_message();
			  comm_stat = 12;
		 }else if(get_buff[5] == 0x03) { /* 获取WIFI版本*/
              uart_resp_wifi_version();
			  comm_stat = 12;
		 }
         get_buff[3]= 0;
         com_timeout = 0;
		 comm_time_2ms = 0; 
	}
}
#endif
#if 1 /* 主板数据转转发发给WIFI */

/** @brief 设置透传WIFI的数据类型的标准位
 */
void Pass_SetFlag(uint8_t f){
   PassFlag |= (1<<f); 
}

void Pass_ResetFlag(uint8_t f){
    PassFlag &= ~(1<<f); 
}

uint8_t Pass_GetFlag(uint8_t f){
	return !!(PassFlag&(1<<f));
}

/** @brief 4字节数组的加法 a=a+b*/
static void ArrayAdd(uint8_t *a, uint8_t *b){
uint16_t sum;
uint8_t ai;	
uint8_t carry = 0;

    for (ai = 4; ai > 0; ai--) {
        sum = a[ai-1] + b[ai-1] + carry;
        a[ai-1] = sum & 0xFF;  // 结果的低8位
        carry = sum >> 8;        // 进位（高8位）
    }
} 

static uint8_t WifiPass(uint8_t *rx_buf){
uint8_t bPass = 0;
uint8_t buf[8];	
	switch(rx_buf[0]){
		case CMD_F0: 
			if(rx_buf[1] == 0){
				memcpy(F0_Data, rx_buf, F0_DATA_SIZE);
				F0_Data[2+3-1] = 0;
				Pass_SetFlag(PASS_F0);
				bPass = 1;
			}
		   break;
		case CMD_F2:
		      if(rx_buf[1] == 1){
				memcpy(buf, rx_buf+2+7-1, 2+6);
				ArrayAdd(buf, rx_buf+2+7-1);
				ArrayAdd(buf+4, rx_buf+2+11-1);

				memcpy(F2_Data, rx_buf, F2_DATA_SIZE); 
				memcpy(F2_Data+2+7-1, buf, 8);
                Pass_SetFlag(PASS_F2);
				bPass = 1;
			  }
		   break;   
	}
	return bPass;
}

/** @brief 透传数据到WIFI，wifi模块调用
 * @return uint8_t  0：没有要转发数据 1：F0, 2:F2
 */
uint8_t WifiForwarding(void){
uint8_t ai = 0;	
uint8_t forward = 0;
    for(ai = 0; ai < PASS_NUM; ai++){
		if(Pass_GetFlag(ai)){
			Pass_ResetFlag(ai);
			if(ai == 0) return CMD_F0;
			else if(ai == 1) return CMD_F2;		
		}
	} 
    return 0;
}
#endif

//������ͨ��1
void comm_process(void)
{
	//uint8_t _old;	
	static uint8_t comm_time_2ms=0;
	// static uint8_t temp_zhengshu_old=0x00,temp_xiaoshu_old=0x00;
	// static uint8_t temp_zhengshu_max_old=0x00,temp_xiaoshu_max_old=0x00;
	// static uint8_t temp_zhengshu_min_old=0x00,temp_xiaoshu_min_old=0x00;
	com_timeout++;
	switch (comm_stat)
	{
		case 0://master send f0 ����������F0���ݲ�ѯ
			com_timeout=0;
			uart_get_F0_message();
			UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
			comm_stat++;
			sendover_flag=1;
		break;
		case 1://slave send ack
		    if(GetCmd(get_buff)==0xf0){
				comm_stat++;
				//aaron,����ǿ�����ģʽ�������ư�f0����������ݵ�?0��9��8�ֽڸ�ֵ��mastercode
				#ifdef OPEN_POWER_FLAG
				openpower_code.master_code[0]= get_buff[10];
				openpower_code.master_code[1] = get_buff[9];
				openpower_code.master_code[2] = get_buff[8];
				#endif 
				WifiPass(get_buff+PACK_DATA_OFFSET);
				Pass_ResetFlag(PASS_F0);
				memset(get_buff,0,sizeof(get_buff));
			}
			//if(rev_flag==1)comm_stat++;
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=0;com_timeout=0;}
			
		break;
		case 2://slave send f1
			if(get_buff[4]==0xf1){
				comm_stat++;
                key_flag = 0;
				F1_Received_Process(get_buff);
				memset(get_buff,0,sizeof(get_buff));
			}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=0;com_timeout=0;}
			
		break;
		case 3://master send f1 ack
			comm_time_2ms++;
			if(comm_time_2ms>=2){
			uart_ack_message();
			UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
			comm_stat=4;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		
		break;

		case 4://master send f1 ��F1���ݲ�ѯ
			if(com_timeout>COMM_TIMEOUT_INIT){
				com_timeout=0;
				uart_get_F1_message();
				UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
				comm_stat++;
				sendover_flag=1;
			}
		break;
		case 5://slave send ack
		        
			if(get_buff[4]==0xf1){comm_stat++;memset(get_buff,0,sizeof(get_buff));}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=4;com_timeout=0;}
			 
		break;
		case 6://slave send f1 ��F1���ݲ�ѯ
			 
			if(get_buff[4]==0xf1){comm_stat++;memset(get_buff,0,sizeof(get_buff));}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=4;com_timeout=0;}
			 
		break;
		case 7://master send f1 ack
			comm_time_2ms++;
		    if(comm_time_2ms>=2){    
			uart_ack_message();
			UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
			comm_stat=8;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		break;
		
		case 8://master send f2   ��ѯF2������
			if(com_timeout>COMM_TIMEOUT_INIT){
				com_timeout=0;
				uart_get_F2_message();
				UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
				comm_stat++;
			}
		break;
		case 9://slave send ack
			if(get_buff[4]==0xf2)
			{
				com_timeout=0;
				comm_stat++;
				WifiPass(get_buff+PACK_DATA_OFFSET);
				Pass_ResetFlag(PASS_F2);
				memset(get_buff, 0, sizeof(get_buff)); 
			}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=8;com_timeout=0;}	 
		break;
		case 10://slave send f1
			if(get_buff[4]==0xf1)comm_stat++;
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=8;com_timeout=0;}

		break;
		case 11://master send f1 ack
			comm_time_2ms++;
		    if(comm_time_2ms>=2){    
			uart_ack_message();
			UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
			comm_stat=12;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		break;
		case 12://master send f4 ��ѯF4������
			if(com_timeout> 15){
                   com_timeout = 0;
				   if(IsFactoryMode()) break;
				   if(key_flag && key_num) key_flag = 0;
					
					if(key_num == 7){  /* Runmode 不需要下传到主板*/
                         if(heater_info.cool_and_hot==0x31){
						    uart_get_F4_message(7); 
						    comm_stat = 13; 
                         }else{
							uart_get_F4_message(3);
							comm_stat = 13;
						 }
						 bHeatTempIgnore = 1;
					 }else if(key_num){
						bHeatTempIgnore = 1;
                        uart_get_F4_message(key_num);
					 	comm_stat = 13; 
				       }else{
					       uart_get_F4_message(3);
						   bHeatTempIgnore = 0;
                           comm_stat = 13;   
				       }
                       //key_num = 0;
				       
				       sendover_flag=1;			
			}else if(get_buff[3]==TEXT_SET && get_buff[4]==0xFF){
				 if(get_buff[5] == 0x00){  /* 进入工程模式 */
                      if(get_buff[6] == 0x31){
					      EnterFactoryMode(0x31);
						  comm_stat = 16;
					  }		  
				 }else if(get_buff[5] == 0x01){ /* 设置LED显示 */
                      display_led(get_buff+6);
					  comm_stat = 16;
				 }
                 get_buff[3]= 0;
                 com_timeout = 0;
			}else if(get_buff[3] == TEXT_GET && get_buff[4] ==0xFF){
			     if(get_buff[5] == 0x02) { /* 获取按键值*/
                      uart_resp_key_message();
					  comm_stat = 12;
				 }else if(get_buff[5] == 0x03) { /* 获取WIFI版本*/
                      uart_resp_wifi_version();
					  comm_stat = 12;
				 }else if(get_buff[5] == 0x04){ /* 获取时间 */
					  uart_resp_time();
					  comm_stat = 12;
				 }
                 get_buff[3]= 0;
                 com_timeout = 0;
				 comm_time_2ms = 0;               
			}
		break;
		case 13://slave send ack
		    if(get_buff[4]==0x3)
                comm_stat = 15;
			
			if(com_timeout>COMM_TIMEOUT){
            comm_stat=12;
            com_timeout=0;
            }
			
		break;
        case 14:   
             uart_get_F4_message(3); 
             comm_stat++;
         break;            
		case 15://slave send f1
			if(com_timeout>COMM_TIMEOUT){
				comm_stat=12;com_timeout=0;
			}
			if(get_buff[4]==0xf1){
				comm_stat++; 
                F1_Received_Process(get_buff);
				memset(get_buff,0,sizeof(get_buff));
			}else{
				if(WifiPass(get_buff+PACK_DATA_OFFSET)){
                    comm_stat++;
				    numberaa++;
				    memset(get_buff,0,sizeof(get_buff));
                }
			}
		break;
		case 16://master send f1 ack
			comm_time_2ms++;
	        if( key_flag==0  )key_num=0;
		 	if(comm_time_2ms>=2){ 
            		
			comm_stat=12;			
			uart_ack_message();
			UART1_IT_SEND_START_function(&uart1_send_flag,pack_buf,pack_buf[2]+6);
			comm_time_2ms=0;
			}
			
		break;
	}
}


uint8_t check_num1(uint8_t pdata2)
{
    uint8_t i=0;
	uint8_t pdata3=0;
    //uint16_t pdata16=0;
	pdata3=pdata2;
    while(pdata2!=0)
    {
        if((pdata2&0x01)==0x01)
        {
          i++;
        }
        pdata2=pdata2>>1;
    }
	if((i==1)||(i==3)||(i==5)||(i==7))
	{
		pdata3=0;
	}
	else
	{
		pdata3=1;
	}
	return pdata3;
}



uint8_t UART1_IT_SEND_START_function(UART1_SEND *uart_send,uint8_t *bpata,uint8_t bpata_len)
{
	if(uart_send->send_flag==SEND1_END)
	{
		uart_send->pdata1=bpata;
		uart_send->pdata_len=bpata_len;
		uart_send->send_len=0;
		uart_send->send_flag=SEND1_RUN;
		if(check_num1(*(uart_send->pdata1+uart_send->send_len)))
        {
          US1CON0 |= 0x08;
        }
        else
        {
          US1CON0 &= 0xf7;
        }
		US1CON3=*(uart_send->pdata1+uart_send->send_len);
		return 1;
	}
	else 
	{
		return 0;
	}
}
uint8_t UART1_IT_SEND_FUNCTION(UART1_SEND *uart_send)
{
	if(uart_send->send_len<(uart_send->pdata_len-1))
	{
		uart_send->send_len=uart_send->send_len+1;

		if(check_num1(*(uart_send->pdata1+uart_send->send_len)))
        {
          US1CON0 |= 0x08;
        }
        else
        {
          US1CON0 &= 0xf7;
        }
		US1CON3=*(uart_send->pdata1+uart_send->send_len);
		return 1;
	}
	else
	{
		uart_send->send_flag=SEND1_END;
		return 0;
	}
}

uint8_t checksum3(uint8_t *buf,uint8_t size1)
{
	uint8_t remainder;
	uint8_t i;
	
	remainder = buf[0];
	
	for (i = 0; i < (size1 - 1); i++)
	{
		remainder ^= buf[i + 1];
	}
	
	return (remainder);
	
}
uint8_t UART_MB_Get_Date(uint8_t date) 
{
	static uint8_t i=0,buf[60]={0};
	static uint8_t numlength=0,numlength1;
	static uint8_t comData=0;
    	if( sendover_flag){sendover_flag=0;i=0;}
    	
	switch (comData)
	{
	case STXdata:
		if(date==stx_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		break;
	case SORTdata:
		if(date==sort_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case LENGTHdata:
		if((date+6)<=60)
		{
			buf[i]=date;
			i++;
			comData++;
			numlength=date;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case COMMANDdata:
		if(date==TEXT_SET||date==TEXT_GET||date==TEXT_RES||date==TEXT_INF||date==TEXT_ACK||date==TEXT_NAK)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		if(numlength==0)
		{
			comData++;
		}
		break;
	case TEXTdata:
		
		if(numlength==1)
		{
			comData++;
		}
		buf[i]=date;
		i++;
		numlength--;
		break;
	case ETXdata:
		//re_date=date;	
		if(date==etx_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case BCCdata:
		numlength1 = checksum3(buf+1,buf[2]+4);
		//re_date=date;
		if(numlength1==date)
		{
			buf[i]=date;
			
			//memset(get_buff,0,sizeof(get_buff));
			memcpy(get_buff,buf,buf[2]+6);
			//memset(buf,0,sizeof(buf));
			commfail_cnt=0;
			Commfail_Flag=0;
			
		}
		comData=STXdata;
		i=0;
		break;
	}
 	return 0;
}


